package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductRelationDTO;
import com.wgh.product.entity.shop.ProductRelation;
import com.wgh.product.service.ProductRelationService;
import com.wgh.product.converter.ProductRelationConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品关联控制器
 * 提供商品关联相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-relation")
public class ProductRelationController {

    @Autowired
    private ProductRelationService productRelationService;
    
    @Autowired
    private ProductRelationConverter productRelationConverter;
    
    /**
     * 根据主商品ID和关联类型查询关联商品列表
     */
    @GetMapping("/main-product/{mainProductId}/type/{relationType}")
    public Result<List<ProductRelationDTO>> getRelationsByMainProductIdAndType(
            @PathVariable Long mainProductId,
            @PathVariable Integer relationType) {
        try {
            List<ProductRelation> productRelations = productRelationService.getRelationsByMainProductIdAndType(mainProductId, relationType);
            List<ProductRelationDTO> productRelationDTOs = productRelationConverter.toDTOList(productRelations);
            return Result.success(productRelationDTOs);
        } catch (Exception e) {
            return Result.error("获取商品关联列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据关联商品ID和关联类型查询关联记录
     */
    @GetMapping("/related-product/{relatedProductId}/type/{relationType}")
    public Result<List<ProductRelationDTO>> getRelationsByRelatedProductIdAndType(
            @PathVariable Long relatedProductId,
            @PathVariable Integer relationType) {
        try {
            List<ProductRelation> productRelations = productRelationService.getRelationsByRelatedProductIdAndType(relatedProductId, relationType);
            List<ProductRelationDTO> productRelationDTOs = productRelationConverter.toDTOList(productRelations);
            return Result.success(productRelationDTOs);
        } catch (Exception e) {
            return Result.error("获取关联商品列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量添加商品关联
     */
    @PostMapping("/batch")
    public Result<Boolean> addBatch(@RequestBody List<ProductRelationDTO> productRelationDTOs) {
        try {
            List<ProductRelation> productRelations = productRelationConverter.toEntityList(productRelationDTOs);
            boolean result = productRelationService.addBatch(productRelations);
            return result ? Result.success(true) : Result.error("批量添加商品关联失败");
        } catch (Exception e) {
            return Result.error("批量添加商品关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据主商品ID删除商品关联
     */
    @DeleteMapping("/main-product/{mainProductId}")
    public Result<Boolean> deleteByMainProductId(@PathVariable Long mainProductId) {
        try {
            boolean result = productRelationService.deleteByMainProductId(mainProductId);
            return result ? Result.success(true) : Result.error("删除商品关联失败");
        } catch (Exception e) {
            return Result.error("删除商品关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据主商品ID和关联类型删除商品关联
     */
    @DeleteMapping("/main-product/{mainProductId}/type/{relationType}")
    public Result<Boolean> deleteByMainProductIdAndType(
            @PathVariable Long mainProductId,
            @PathVariable Integer relationType) {
        try {
            boolean result = productRelationService.deleteByMainProductIdAndType(mainProductId, relationType);
            return result ? Result.success(true) : Result.error("删除指定类型的商品关联失败");
        } catch (Exception e) {
            return Result.error("删除指定类型的商品关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 检查商品关联是否存在
     */
    @GetMapping("/check")
    public Result<Boolean> checkRelationExists(
            @RequestParam Long mainProductId,
            @RequestParam Long relatedProductId,
            @RequestParam Integer relationType) {
        try {
            boolean exists = productRelationService.checkRelationExists(mainProductId, relatedProductId, relationType);
            return Result.success(exists);
        } catch (Exception e) {
            return Result.error("检查商品关联是否存在失败：" + e.getMessage());
        }
    }
    
    /**
     * 添加单条商品关联
     */
    @PostMapping
    public Result<Boolean> addProductRelation(@RequestBody ProductRelationDTO productRelationDTO) {
        try {
            ProductRelation productRelation = productRelationConverter.toEntity(productRelationDTO);
            boolean result = productRelationService.save(productRelation);
            return result ? Result.success(true) : Result.error("添加商品关联失败");
        } catch (Exception e) {
            return Result.error("添加商品关联失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除商品关联
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProductRelation(@PathVariable Long id) {
        try {
            boolean result = productRelationService.removeById(id);
            return result ? Result.success(true) : Result.error("删除商品关联失败");
        } catch (Exception e) {
            return Result.error("删除商品关联失败：" + e.getMessage());
        }
    }
}